﻿#region | Copyright © 2556 |

/*
##########################################################################
	Author By : Siriwat Suananpornpanit
	DateTime : 2556/08/30
##########################################################################
*/

#endregion

#region | Imports |

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccess;
using Web.Business;
using Web.Core;
using Exception = System.Exception;

#endregion

namespace Web.Modal
{
    public partial class ApplicationModal : System.Web.UI.UserControl
    {

        #region | Event Paging |

        #region | Const |

        public DataAccessClient dal = new DataAccessClient(DATABASE_PROXY.AUTH);

        private p_USER_PROFILE _userPartial;//= Core.SessionPool.GetInstance().GetSession();

        public object result = null;

        public String message = null;

        private bool _exception = false;

        #endregion

        #region | InitAccess |

        public void InitAccess()
        {
            // Init Access
            AccessToolbar.Resource = "Resource";
            AccessToolbar.SaveText = "Save";
            AccessToolbar.CancelText = "Cancel";
            AccessToolbar.AddVisible = false;
            AccessToolbar.EditVisible = false;
            AccessToolbar.CopyVisible = false;
            AccessToolbar.DeleteVisible = false;
            AccessToolbar.SearchVisible = false;
            //
            AccessToolbar.SaveToolTip = "Save";
            AccessToolbar.CancelToolTip = "CancelOrUndo";
            //
            AccessToolbar.SaveClick += OnSaveClick;
            AccessToolbar.CancelClick += OnCancelClick;
            AccessToolbar.CancelAttributes.Add("OnClick", "pcModal.Hide();");
        }

        private void OnCancelClick(object sender, EventArgs e)
        {
            Clear();
        }

        private void OnSaveClick(object sender, EventArgs e)
        {
            String result = OnValidation();

            if(String.IsNullOrEmpty(result))
            {
                string flag = hdnFlag.Value;

                p_APPLICATION obj = new p_APPLICATION();
                obj.APPLNAME = txApplicationName.Text;
                obj.COMPANY_CODE = _userPartial.COMP_CODE;
                obj.DESCRIPTION = txDescription.Text;
                obj.URL = txUrl.Text;
                obj.PDAFLAG = (chkMobile.Checked) ? "Y" : "N";
                obj.MOBILEURL = txMobileUrl.Text;
                obj.STATUS = (rdoStatusUse.Checked) ? "Y" : (rdoStatusNotUse.Checked) ? "N" : null;
                obj.PRIVATE_FLAG = (chkPrivate.Checked) ? "Y" : "N";
                obj.BUYER_FLAG = (chkSpecifyBuyer.Checked) ? "Y" : "N";
                obj.MC_FLAG = (chkSpecifyMc.Checked) ? "Y" : "N";
                obj.REMARK = txRemark.Text;

                if (flag == AccessFlag.Add.ToString())
                {
                    obj.APPLNO = ApplicationBal.GetInstance().GetApplAutoGen(this.Page, obj.COMPANY_CODE);
                    obj.CRDATE = DateTime.Now;
                    obj.CRBY = _userPartial.EMPNO;
                    ApplicationBal.GetInstance().Insert(obj,out message);

                    if (String.IsNullOrEmpty(message)) { message = ResourceTools.GetResource(resourceKey: "InsertCompleted"); }
                    else _exception = true;
                }

                else if (flag == AccessFlag.Copy.ToString())
                {
                    obj.APPLNO = ApplicationBal.GetInstance().GetApplAutoGen(this.Page, obj.COMPANY_CODE);
                    obj.CRDATE = DateTime.Now;
                    obj.CRBY = _userPartial.EMPNO;
                    ApplicationBal.GetInstance().Insert(obj, out message);
                    if (String.IsNullOrEmpty(message)) { message = ResourceTools.GetResource(resourceKey: "InsertCompleted"); }
                    else _exception = true;
                }

                else if(flag == AccessFlag.Edit.ToString())
                {
                    obj.APPLNO = txApplicationNo.Text;
                    obj.UPDDATE = DateTime.Now;
                    obj.UPDBY = _userPartial.EMPNO;
                    ApplicationBal.GetInstance().Update(obj, out message);
                    if (String.IsNullOrEmpty(message))
                    {
                        message = ResourceTools.GetResource(resourceKey: "UpdateCompleted");
                    }
                    else _exception = true;
                }

                // Call Methods in MainPage Application
                    if (_exception){
                        //
                        Core.Modal.Exception(
                           page: this.Page,
                           message: Core.Exception.ModalException(
                                               method: System.Reflection.MethodBase.GetCurrentMethod(),
                                               ex: new SystemException(message)),
                           headerText: hdnHeader.Value);
                        //
                    }
               
                    else{
                        Clear();
                        Core.Modal.Toggle(
                                        page: this.Page,
                                        modal: ModalFlag.Hide,
                                        flag: MessageFlag.Completed,
                                        massage: message,
                                        headerText: hdnHeader.Value);
                       }
            }
            else
            {
                Core.Modal.Toggle(
                                page:this.Page ,
                                modal: ModalFlag.Show, 
                                flag: MessageFlag.Wraning,
                                massage: result,
                                headerText: hdnHeader.Value);
            }
            
        }

        #endregion
        
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                _userPartial = Core.SessionPool.GetInstance().GetSession(this.Page);
                InitAccess();
            }
            catch (System.Exception ex)
            {
               //
               Core.Modal.Exception(
                  page: this.Page,
                  message: Core.Exception.ModalException(
                                      method: System.Reflection.MethodBase.GetCurrentMethod(),
                                      ex: ex),
                  headerText: hdnHeader.Value);
                //
            }
        }

        #endregion

        #region | Methods |

        public void OnLoad(
                            AccessFlag flag,
                            string applNo = null,
                            String headerText = null,
                            string companyCode = "HP00")
        {
            try
            {
                Clear();
                //
                if (flag == AccessFlag.Add)
                {
                    txApplicationNo.Text = ApplicationBal.GetInstance()
                        .GetApplAutoGen(this.Page, companyCode);
                
                    hdnFlag.Value = flag.ToString();

                    hdnHeader.Value = headerText;

                    txApplicationNo.Attributes.Add("readonly", "readonly");

                    Page.ClientScript.RegisterStartupScript(
                                                    type: typeof(bool),
                                                    key: "130B00E7-2BA0-4022-AC28-3B2945109249",
                                                    script: "pcModal.Show();",
                                                    addScriptTags: true);
                }
                else if (flag == AccessFlag.Copy || flag == AccessFlag.Edit)
                {
                    p_APPLICATION o = ApplicationBal.GetInstance().Select(page: this.Page, obj: new p_APPLICATION()
                                                                            {
                                                                                APPLNO = applNo
                                                                                ,
                                                                                COMPANY_CODE = companyCode
                                                                            }).First();
                    if (o != null)
                    {
                        if (flag == AccessFlag.Copy){ txApplicationNo.Text = ApplicationBal.GetInstance()
                                                            .GetApplAutoGen(this.Page, companyCode);}
                        else {txApplicationNo.Text = o.APPLNO;}
                        txApplicationName.Text =o.APPLNAME;
                        txDescription.Text = o.DESCRIPTION;
                        txUrl.Text = o.URL;
                        chkMobile.Checked = (o.PDAFLAG == "Y") ? true : false;
                        txMobileUrl.Text = o.MOBILEURL;

                        if (o.STATUS == "Y")
                            rdoStatusUse.Checked = true;
                        else
                            rdoStatusNotUse.Checked = true;

                        chkPrivate.Checked = (o.PRIVATE_FLAG == "Y") ? true : false;
                        chkSpecifyBuyer.Checked = (o.BUYER_FLAG == "Y") ? true : false;
                        chkSpecifyMc.Checked = (o.MC_FLAG == "Y") ? true : false;
                        txRemark.Text = o.REMARK;
                    }

                hdnFlag.Value = flag.ToString();

                hdnHeader.Value = headerText;

                txApplicationNo.Attributes.Add("readonly", "readonly");
            
                Page.ClientScript.RegisterStartupScript(
                                                    type: typeof(bool),
                                                    key: "130B00E7-2BA0-4022-AC28-3B2945109249",
                                                    script: "pcModal.Show();",
                                                    addScriptTags: true);
                }
                else
                {
                    Core.Modal.Toggle(
                                    page: this.Page, 
                                    modal:ModalFlag.Hide,  
                                    flag: MessageFlag.None, 
                                    massage:null,
                                    headerText: hdnHeader.Value);
                }
            }
            catch (System.Exception ex)
            {
                //
                Core.Modal.Exception(
                   page: this.Page,
                   message: Core.Exception.ModalException(
                                       method: System.Reflection.MethodBase.GetCurrentMethod(),
                                       ex: ex),
                   headerText: hdnHeader.Value);
                //
            }
        }

        private string OnValidation()
        {
            StringBuilder sb ;//= new StringBuilder();
            try
            {
                sb = new StringBuilder();
                string line = "<br/><br/>";
                //
                if (String.IsNullOrEmpty(txApplicationNo.Text))
                    sb.AppendFormat("- {0}{1}",
                        String.Format(ResourceTools.GetResource(resourceKey: "requireInputFormat"),
                                        ResourceTools.GetResource(resourceKey: "ApplicationNo")), line);

                if (String.IsNullOrEmpty(txApplicationName.Text))
                    sb.AppendFormat("- {0}{1}",
                        String.Format(ResourceTools.GetResource(resourceKey: "requireInputFormat"),
                                        ResourceTools.GetResource(resourceKey: "ApplicationName")), line);

                if (String.IsNullOrEmpty(txDescription.Text))
                    sb.AppendFormat("- {0}{1}",
                        String.Format(ResourceTools.GetResource(resourceKey: "requireInputFormat"),
                                        ResourceTools.GetResource(resourceKey: "Description")), line);

                if (String.IsNullOrEmpty(txUrl.Text))
                    sb.AppendFormat("- {0}{1}",
                        String.Format(ResourceTools.GetResource(resourceKey: "requireInputFormat"),
                                        ResourceTools.GetResource(resourceKey: "URL")), line);

                if (String.IsNullOrEmpty(txMobileUrl.Text))
                    sb.AppendFormat("- {0}{1}",
                        String.Format(ResourceTools.GetResource(resourceKey: "requireInputFormat"),
                                        ResourceTools.GetResource(resourceKey: "MobileURL")), line);
                //
                return sb.ToString();
            }
            catch (System.Exception ex)
            {
                //
                Core.Modal.Exception(
                   page: this.Page,
                   message: Core.Exception.ModalException(
                                       method: System.Reflection.MethodBase.GetCurrentMethod(),
                                       ex: ex),
                   headerText: hdnHeader.Value);
                //
                return string.Format("?");
            }
        }

        private void Clear()
        {
            txApplicationName.Text = string.Empty;
            txDescription.Text = string.Empty;
            txUrl.Text = string.Empty;
            chkMobile.Checked = false;
            txMobileUrl.Text = string.Empty;
            rdoStatusUse.Checked = true;
            chkPrivate.Checked = false;
            chkSpecifyBuyer.Checked = false;
            chkSpecifyMc.Checked = false;
            txRemark.Text = string.Empty;
            hdnFlag.Value = string.Empty;
            hdnHeader.Value = string.Empty;
        }

        #endregion

    }
}